﻿using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using FairyGUI;
using Baiyy.Controls;

namespace Baiyy {
	/// <summary>
	/// UI控制器。
	/// </summary>
	public class UISystem {

		#region types
		public delegate void LoadPackageDelegate ();
		#endregion

		#region vars
		/// <summary>
		/// 全局静态对象
		/// </summary>
		internal static UISystem _inst;

		/// <summary>
		/// 主UI层
		/// </summary>
		GComponent _mainUILayer;
		/// <summary>
		/// 弹出UI层
		/// </summary>
		GComponent _popupUILayer;
		/// <summary>
		/// 掩码UI层
		/// </summary>
		GComponent _maskUILayer;
		/// <summary>
		/// 调试UI层
		/// </summary>
		GComponent _debugUILayer;

		/// <summary>
		/// 主UI组件
		/// </summary>
		BaseWindow _mainUI;

		/// <summary>
		/// 弹出窗口组件
		/// </summary>
		List<BaseWindow> _popupUIs;

		/// <summary>
		/// 加载UI包代理
		/// </summary>
		LoadPackageDelegate _loadPackage;

		#endregion

		#region getter&setter

		public static UISystem inst { 
			get {
				if (_inst == null) {
					_inst = new UISystem ();
				}
				return _inst; 
			} 
		}

		public LoadPackageDelegate onLoadPackage {
			get {
				return _loadPackage;
			}
			set {
				_loadPackage = value;
			}
		}

		#endregion

		#region methods

		public UISystem() {
			_mainUILayer = _popupUILayer
				= _maskUILayer = _debugUILayer = null;

			_mainUI = null;
			_popupUIs = new List<BaseWindow> ();
		}

		/// <summary>
		/// 初始化。
		/// </summary>
		public void Init(int designWidth, int designHeight) {
			// 设置UI缩放系数
			GRoot.inst.SetContentScaleFactor (designWidth, designHeight);

			// 初始化UI层组件
			_mainUILayer = new GComponent ();
			_mainUILayer.gameObjectName = "MainUILayer";
			GRoot.inst.AddChild (_mainUILayer);

			_popupUILayer = new GComponent ();
			_popupUILayer.gameObjectName = "PopupUILayer";
			GRoot.inst.AddChild (_popupUILayer);

			_maskUILayer = new GComponent ();
			_maskUILayer.gameObjectName = "MaskUILayer";
			GRoot.inst.AddChild (_maskUILayer);

			_debugUILayer = new GComponent ();
			_debugUILayer.gameObjectName = "DebugUILayer";
			GRoot.inst.AddChild (_debugUILayer);

			// 加载UI包
			if (_loadPackage != null) {
				_loadPackage ();
			}
		}

		/// <summary>
		/// 释放UI系统。
		/// </summary>
		public void Uninit() {
			UIPackage.RemoveAllPackages ();
		}

		/// <summary>
		/// 显示窗口。
		/// </summary>
		/// <param name="win">Window.</param>
		public void Show(BaseWindow win) {
			if (_mainUI != null) {
				_mainUI.Dispose ();
			}
			_mainUI = win;
			if (_mainUI != null) {
				_mainUI.Attach (_mainUILayer);
			}
		}

		/// <summary>
		/// 显示弹出窗口。
		/// </summary>
		/// <param name="win">Window.</param>
		/// <param name="closePop">If set to <c>true</c> 将会关闭已经打开的弹出窗口.</param>
		public void Popup(BaseWindow win, bool closePop) {
			if (closePop) {
				while (_popupUIs.Count > 0) {
					BaseWindow dispWin = _popupUIs [0];
					_popupUIs.RemoveAt (0);
					dispWin.Dispose ();
				}
			}

			if (win != null) {
				win.Attach (_popupUILayer);
				_popupUIs.Add (win);
			}
		}
		#endregion
	}
}
